package be.jabapage.snooker.service.importation.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.Validate;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import be.jabapage.snooker.container.ClubContainer;
import be.jabapage.snooker.container.TeamContainer;
import be.jabapage.snooker.jdo.administration.Club;
import be.jabapage.snooker.jdo.administration.Player;
import be.jabapage.snooker.jdo.administration.Team;
import be.jabapage.snooker.service.administration.api.IFormatParser;
import be.jabapage.snooker.service.importation.api.IExcelClubParser;

public class ExcelClubParser implements IExcelClubParser {

	private final IFormatParser formatParser;

	public ExcelClubParser(final IFormatParser formatParser) {
		Validate.notNull(formatParser);
		this.formatParser = formatParser;
	}

	public Set<ClubContainer> parse(final Workbook workBook) {
		Set<ClubContainer> result = new HashSet<ClubContainer>();
		Map<String, ClubContainer> clubMap = new HashMap<String, ClubContainer>();
		Sheet invulSheet = workBook.getSheet("invul");
		int index = 4;

		while (true) {
			Row row = invulSheet.getRow(index++);
			Cell codeCell = row.getCell(1); //column with the code inside
			if (codeCell == null) {
				continue;
			}
			String clubCode = null;
			if (codeCell.getCellType() == 0) {
				clubCode = Double.toString(codeCell.getNumericCellValue()).substring(0,4);
			} else {
				clubCode = codeCell.getStringCellValue();
			}
			if (clubCode != null && clubCode.length() > 4) {
				break; //we start with the players now.
			}

			ClubContainer clubContainer = new ClubContainer();
			Cell nameCell = row.getCell(3);
			String teamName = nameCell.getStringCellValue();
			String clubName = teamName.substring(0, teamName.length() - 2);
			if (!clubMap.containsKey(clubCode)) {
				clubMap.put(clubCode, clubContainer);
				Club club = new Club();
				club.setCode(clubCode);
				club.setName(clubName);
				clubContainer.setClub(club);
			} else {
				clubContainer = clubMap.get(clubCode);
			}

			//adding the team to the club
			TeamContainer teamContainer = new TeamContainer();
			clubContainer.getTeams().put(teamName, teamContainer);
			Team team = new Team();
			team.setName(teamName);
			teamContainer.setTeam(team);
		}

		//adding the players 
		index--; // index was one to far.
		while (true) {
			Row row = invulSheet.getRow(index++);
			if (row == null) {
				continue;
			}
			Cell codeCell = row.getCell(1);
			if (codeCell == null || codeCell.getCellType() == 3) {
				continue;
			}
			String clubCode = null;
			String playerCode = null;
			String playerNumber = null;
			if (codeCell.getCellType() == 0) {
				break;
			} else {
				playerNumber = codeCell.getStringCellValue();
				clubCode = formatParser.parseClubCode(playerNumber );
				playerCode = formatParser.parsePlayerCode(playerNumber );
			}

			Player player = new Player();
			String playerName = row.getCell(2).getStringCellValue();
			player.setFirstName(formatParser.parseFirstName(playerName));
			player.setName(formatParser.parseLastName(playerName));
			player.setNumber(playerCode);
			ClubContainer club = clubMap.get(clubCode);
			if (formatParser.isBackupPlayer(playerNumber )) {
				club.getBackupPlayers().put(playerCode, player);
			} else {
				String teamName = club.getClub().getName() + " " + formatParser.parseTeamNumber(playerNumber);
				club.getTeams().get(teamName).getMembers().put(playerCode, player);
			}

		}

		for (String key : clubMap.keySet()) {
			result.add(clubMap.get(key));
		}
		return result;
	}

}
